<template>
  <a-modal
    :title="title"
    :width="800"
    :ok=false
    :visible="visible"
    :confirmLoading="confirmLoading"
    :okButtonProps="{ props: {disabled: disableSubmit} }"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="refundModel" :rules="validatorRules">
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="订单号">
          <span>{{model.orderInfoNo}}</span>
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="订单名称">
          <span>{{model.orderInfoTitle}}</span>
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="订单状态">
          <span>{{refundModel.refundOrderStatusText}}</span>
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="退款金额">
          <span>￥{{refundModel.refundPrice/100}}</span>
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="退款原因">
          <span>{{refundModel.refundReason}}</span>
        </a-form-model-item>
        <template v-if="refundModel.refundOrderStatus == 8">
          <a-form-model-item 
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="发货时间">
            <span>{{logisticsModel.logisticsTime}}</span>
          </a-form-model-item>
          <a-form-model-item 
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="物流公司">
            <span>{{logisticsModel.logisticsName}}</span>
          </a-form-model-item>
          <a-form-model-item 
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="物流单号">
            <span>{{logisticsModel.logisticsNo}}</span>
          </a-form-model-item>
        </template>
        <a-form-model-item 
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="refundStatus"
          label="审核">
          <a-radio-group v-model="refundModel.refundStatus">
            <a-radio :value="1">同意</a-radio>
            <a-radio :value="2">拒绝</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item 
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="审核时间">
          <span>{{refundModel.refundApproveTime}}</span>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import { getAction, postAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from 'moment'

  export default {
    name: "LogisticsModal",
    data () {
      return {
        title:"退款审核",
        visible: false,
        disableSubmit:false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
          refundStatus:[{ required: true, message: '请选择审核结果!' }]
        },
        refundModel: {},
        logisticsModel: {}
      }
    },
    created () {
    },
    methods: {
      loadRefundData(orderId){
        var that = this;
        getAction('/shop/order/refund/detail', {orderId}).then((res)=>{
          if(res.success){
            this.refundModel = res.result
          } else {
            this.$message.error(res.message)
            setTimeout(_ => {
              this.close()
            }, 1500)
          }
        })
      },
      loadLogisticsData(orderId){
        var that = this;
        getAction('/shop/order/logistics', {orderId}).then((res)=>{
          if(res.success){
            this.logisticsModel = res.result
          } else {
            this.$message.error(res.message)
            setTimeout(_ => {
              this.close()
            }, 1500)
          }
        })
      },
      async edit (record) {
        console.log(record)
          this.visible = true;
          this.model = Object.assign({}, record)
          await this.loadRefundData(record.dataId);
          await this.loadLogisticsData(record.dataId);
      },
      close () {
        this.$emit('close');
        this.disableSubmit = false;
        this.visible = false;
        this.$refs.form.resetFields();
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let data = {
              dataId: this.refundModel.dataId,
              status: parseInt(this.refundModel.refundStatus)
            }
            postAction('/shop/order/refund/verify', data).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })

          }else{
            return false;
          }
        })
      },
      handleCancel () {
        this.close()
      }
    }
  }
</script>

<style scoped>

</style>